package com.xyyl.patient.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.service.WxPayService;
import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.controller.BaseController;
import com.xyyl.patient.core.support.Pager;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.entity.continuationonline.ContinuationOnline;
import com.xyyl.patient.entity.mall.MallOrderInfo;
import com.xyyl.patient.entity.mall.MallOrderRefund;
import com.xyyl.patient.service.mall.MallOrderInfoService;
import com.xyyl.patient.service.mall.MallOrderRefundService;

/**
 * @projectName:xyyl-patient-web
 * @ClassName: MallOrderRefundController
 * @Description: 退款管理
 * @author wqs
 * @date 2018年3月6日
 *
*/
@RestController
@RequestMapping("/mallOrderRefund")
public class MallOrderRefundController extends BaseController {
	
	private Logger logger = LogManager.getLogger(MallOrderRefundController.class);

	@Autowired
	private MallOrderRefundService mallOrderRefundService;
	@Autowired
	private MallOrderInfoService mallOrderInfoService;
	/**
	 * 
	 * @Title: insertMallOrderRefund
	 * @Description: 退货申请
	 * @param @param MallOrderRefund
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/insertMallOrderRefund", method = RequestMethod.POST)
	public JSONObject insertMallOrderRefund(MallOrderRefund MallOrderRefund) {
		logger.info(" insert MallOrderRefund:{}", MallOrderRefund);
		try {
			//如果订单不存在，退货表不能新入插订单
			MallOrderInfo mallOrderInfo = mallOrderInfoService.queryObject(MallOrderRefund.getOrderId());
			if (null == mallOrderInfo) {
				return ResponseBodyResult.success("申请不成功");
			}
			if (4 == mallOrderInfo.getOrderStatus()) {
				return ResponseBodyResult.fail("已提交申请，等待处理");
			}
			//退货表新插入一条记录
			return ResponseBodyResult.success("申请成功", mallOrderRefundService.saveMallOrderRefund(MallOrderRefund,mallOrderInfo));

		} catch (Exception e) {
			return ResponseBodyResult.error("申请失败", e);
		}
	}
	
	/**
	 * 
	 * @Title: updateMallOrderRefund
	 * @Description: 退货---选择快递公司
	 * @param @param MallOrderRefund
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/updateMallOrderRefund", method = RequestMethod.POST)
	public JSONObject updateMallOrderRefund(MallOrderRefund MallOrderRefund) {
		logger.info(" update MallOrderRefund:{}", MallOrderRefund);
		try {
			//退货无记录
			MallOrderRefund mallOrderRefund = mallOrderRefundService.queryByOrderId(MallOrderRefund.getOrderId());
			if (null == mallOrderRefund) {
				return ResponseBodyResult.success("提交不成功","退货单号不正确");
			}
			MallOrderRefund.setId(mallOrderRefund.getId());
			int result = mallOrderRefundService.updateMallOrderRefund(MallOrderRefund);
			if (result>0) {
				//退货表更新物流状态
				return ResponseBodyResult.success("提交成功", result);
			} else {
				return ResponseBodyResult.fail("提交失败");
			}
			

		} catch (Exception e) {
			return ResponseBodyResult.error("提交失败", e);
		}
	}
	
	/**
	 * 
	 * @Title: insertMallOrderRefund
	 * @Description: 退货查询
	 * @param @param MallOrderRefund
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/queryMallOrderRefund", method = RequestMethod.POST)
	public JSONObject queryMallOrderRefund() {
		Pager pager = super.initPage();
		try {
			return ResponseBodyResult.success("查询成功", mallOrderRefundService.queryList(pager));

		} catch (Exception e) {
			return ResponseBodyResult.error("查询失败", e);
		}
	}
	
	/**
	 * 
	 * @Title: get
	 * @Description: 退货查询
	 * @param @param MallOrderRefund
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public JSONObject getById(@RequestParam Integer orderId) {
		try {
			MallOrderRefund orderRefund = mallOrderRefundService.queryByOrderId(orderId);
			if (null != orderRefund) {
				return ResponseBodyResult.success("查询成功", orderRefund);
			}else {
				return ResponseBodyResult.fail("暂无退货订单");
			}
			

		} catch (Exception e) {
			return ResponseBodyResult.error("查询失败", e);
		}
	}
}
